package com.controller;


import com.controller.reqdto.AddUserParam;
import com.controller.reqdto.LoginParam;
import com.controller.reqdto.UpdateUserParam;
import com.controller.respdto.UserLoginResp;
import com.controller.template.ResultHelper;

import com.controller.template.ResultInfo;
import com.service.UserService;
import com.util.ExportSql;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;


/**
 * @author ruiwen
 */
@RestController
@Api(tags = "用户模块")
@CrossOrigin
@RequestMapping("/user")
public class UserController extends ResultHelper {
    @Autowired
    private UserService userService;
    @Resource
    private ExportSql exportSql;

    /**
     * 登陆
     *
     * @param loginParam
     * @return
     */
    @ApiOperation("账号密码登陆")
    @PostMapping("/login")
    public ResultInfo login(@RequestBody LoginParam loginParam) {
        int login = userService.login(loginParam);
        if (login <= 0) {
            return failResultInfo(403, "账号或密码错误");
        }
        return successResultInfo(userService.getUserMessage(loginParam));

    }
    /**
     * 登陆
     *
     * @param
     * @return
     */
    @ApiOperation("获取用户列表")
    @GetMapping("/getUserList")
    public ResultInfo getUserList() {
        return successResultInfo(userService.getUserList());
    }
    /**
     * 更新用户信息
     *
     * @param
     * @return
     */
    @ApiOperation("更新用户信息")
    @PostMapping("/updateUserMsg")
    public ResultInfo updateUserMsg(@RequestBody  UpdateUserParam updateUserParam) {
        return successResultInfo(userService.updateUserMsg(updateUserParam));
    }
    /**
     * 删除用户信息
     *
     * @param
     * @return
     */
    @ApiOperation("删除用户信息")
    @PostMapping("/deleteUserMsg")
    public ResultInfo updateUserMsg(Integer userId) {
        return successResultInfo(userService.getBaseMapper().deleteById(userId));
    }


    /**
     * 添加用户
     *
     * @param addUserParam
     * @return
     */
    @ApiOperation("添加用户")
    @PostMapping("/addUser")
    public ResultInfo addUser(@RequestBody AddUserParam addUserParam) {
        if (Objects.isNull(addUserParam) || Objects.isNull(addUserParam.getUserName()) || Objects.isNull(addUserParam.getPassword())) {
            return failResultInfo(500, "参数错误(用户名或密码为空))");
        }
        return userService.addUser(addUserParam) ? successResultInfo("添加成功") : failResultInfo(500, "添加失败");

    }



    /**
     * 添加用户
     *
     * @param
     * @return
     */
    @ApiOperation("备份")
    @GetMapping("/export")
    public ResultInfo export() {


        exportSql.export();
        return successResultInfo("备份成功");

    }
}
